package fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.game.demo409.demo409.R;
import com.yalantis.pulltorefresh.library.PullToRefreshView;

import java.util.LinkedList;
import android.os.Handler;

import fragments.FragmentAbstract.FindingInFmBase;
import views.LoadMoreListView;

/**
 * Created by Administrator on 2015/2/4.
 */
public class FindingMyself extends FindingInFmBase{
    private static final String ARG_POSITION = "position";

    private  static final int REFRESH_DELAY = 2000;
    private  static final int LOAD_DELAY = 2000;
    private  int position;

    private PullToRefreshView mPullToRefreshView;

    private LoadMoreListView mListView;

    private ArrayAdapter<String> mAdapter;

    private LinkedList<String> data ;

    private static Handler handler = new Handler();

    private  boolean isPrepared = false;

    public  static  FindingMyself newInstance(int position) {
        FindingMyself fa = new FindingMyself();
        Bundle b = new Bundle();
        b.putInt(ARG_POSITION,position);
        fa.setArguments(b);
        return fa;
    }
    @Override

    public  void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        position = getArguments().getInt(ARG_POSITION);
        data = new LinkedList<String>();
        data.add("我的活动1");
        data.add("我的活动2");
        data.add("我的活动3");
        data.add("我的活动4");
        data.add("我的活动5");
        data.add("我的活动6");
        data.add("我的活动7");
        data.add("我的活动8");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View myAct = inflater.inflate(R.layout.findingmine_fm,container,false);

        mListView = (LoadMoreListView)myAct.findViewById(R.id.mine_list);
        // all_list.setAdapter(new FindingallAdapter(list,getActivity()));
        mAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_expandable_list_item_1,data);
        mListView.setOnGetMoreListener(new LoadMoreListView.OnGetMoreListener() {
            @Override
            public void onGetMore() {
                mPullToRefreshView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        data.add("加载+");
                        mAdapter.notifyDataSetChanged();
                        mListView.getMoreComplete();
                    }
                },LOAD_DELAY);
            }
        });
        mListView.setAdapter(mAdapter);

        mPullToRefreshView = (PullToRefreshView)myAct.findViewById(R.id.pull_to_refresh_mine);
        mPullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
            @Override
            public  void  onRefresh() {
                mPullToRefreshView.postDelayed( new Runnable() {
                    @Override
                    public void run() {
                        data.addFirst("我的新活动");
                        mAdapter.notifyDataSetChanged();
                        mPullToRefreshView.setRefreshing(false);
                    }
                },REFRESH_DELAY);

            }

        });


        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });

        isPrepared = true;

        mListView.performGetMore();

        FragmentLoading();

        return myAct;

    }
    @Override
    protected  void FragmentLoading() {
        if(!isPrepared||!isVisible) {
            return;
        }
        //Fragment可见就刷新
        mPullToRefreshView.setRefreshing(true);
        new Thread (new Runnable() {
            @Override
            public void run() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        data.addFirst("我的新活动");
                        mAdapter.notifyDataSetChanged();
                        mPullToRefreshView.setRefreshing(false);
                    }
                },REFRESH_DELAY);
            }
        }).start();
    }
}
